package com.cskaoyan.service;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.AdminExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.Log;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.LogMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
@Service
public class AdminAdminServiceImpl implements AdminAdminService{
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    LogMapper logMapper;
    @Override
    public ListData<Admin> list(Integer page, Integer limit, String username,String sort, String order) {
        PageHelper.startPage(page, limit);
        AdminExample adminExample=new AdminExample();
        adminExample.setOrderByClause(sort+" "+order);
        AdminExample.Criteria criteria=adminExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        ListData<Admin> listData=new ListData<>();
        listData.setItems(admins);
        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        long total=adminPageInfo.getTotal();
        listData.setTotal((int) total);
        return listData;
    }
    @Override
    public Admin create(Admin admin,String currentUsername) {
        adminMapper.insertSelective(admin);
        Log log=new Log();
        try {
            String IP= InetAddress.getLocalHost().getHostAddress();
            log.setAdmin(currentUsername);
            log.setIp(IP);
            log.setType(1);
            log.setAction("新建用户");
            log.setStatus(true);
            log.setResult(admin.getUsername());
            int i = logMapper.insertSelective(log);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return admin;
    }
    @Override
    public Admin info(Integer userId) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        if (userId != null) {
            criteria.andIdEqualTo(userId);
        }
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        return admins.get(0);
    }

    @Override
    public int selectUserId(String username) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        if (username != null) {
            criteria.andUsernameEqualTo(username);
        }
        int userId = adminMapper.selectUserIdByName(username);
        return userId;
    }

    @Override
    public int delete(Admin admin,String currentUsername) {
        Log log = new Log();
        int row=adminMapper.deleteByPrimaryKey(admin.getId());
        String IP= null;
        try {
            IP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        log.setAdmin(currentUsername);
        log.setIp(IP);
        log.setType(1);
        log.setAction("删除用户");
        log.setStatus(true);
        log.setResult(admin.getUsername());
        logMapper.insertSelective(log);
        return row;
    }

    @Override
    public Admin update(Admin admin, String currentUsername) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        if (admin.getId() != null) {
            criteria.andIdEqualTo(admin.getId());
        }
        adminMapper.updateByExampleSelective(admin,adminExample);
        Log log = new Log();
        String IP= null;
        try {
            IP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        log.setAdmin(currentUsername);
        log.setIp(IP);
        log.setType(1);
        log.setAction("更改用户");
        log.setStatus(true);
        log.setResult(admin.getUsername());
        int i = logMapper.insertSelective(log);
        return admin;
    }
}
